#include "qmytimeedit.h"

#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"
#include "../../include/qcommonstruct.h"

#include <QVariant>
#include <QMouseEvent>

QTimeEditHost::QTimeEditHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QTimeEditHost::hour()
{
    return ((QTimeEdit*)m_widget)->time().hour();
}

int QTimeEditHost::minute()
{
    return ((QTimeEdit*)m_widget)->time().minute();
}

int QTimeEditHost::second()
{
    return ((QTimeEdit*)m_widget)->time().second();
}

void QTimeEditHost::setTime(int hour, int minute, int second)
{
    ((QTimeEdit*)m_widget)->setTime(QTime(hour,minute,second));
}

QMyTimeEdit::QMyTimeEdit(QWidget *parent):
    QTimeEdit(parent),
    m_timer(new QTimer(this)),
    m_host(new QTimeEditHost(this))
{
    setProperty("className","QTimeEdit");
    setProperty("typeName","Time Edit");
    setProperty("typeIcon",":/images/widgets/timeedit.png");
    setTime(QTime::currentTime());

    m_host->addProperty("Attribute",QStringList()<<"time"<<"auto-running"<<"focusPolicy"<<"font");
    m_host->addProperty("Signals",QStringList()<<"timeChanged");
    m_host->addUserProperty(QStringList()<<"time"<<"auto-running");


    connect(m_timer,SIGNAL(timeout()),this,SLOT(timer()));

    m_timer->start(300);

    connect(this,SIGNAL(timeChanged(QTime)),this,SLOT(timeChanged_signals(QTime)));
}

void QMyTimeEdit::timer()
{
    if(property("auto-running").toBool())
    {
        if(property("running").toBool())
        {
            setProperty("time",QTime::currentTime());
        }
        else
        {
            PropertyList list=property("property_list").value<PropertyList>();
            foreach(QAbstractProperty *p,list)
            {
                if(p->name()=="time")
                {
                    p->setValue(QTime::currentTime());
                }
            }
        }
    }
}


void QMyTimeEdit::timeChanged_signals(const QTime &time)
{
    QString code=property("timeChanged").toString();
    if(code!="")
    {
        QString para=QString("type=timeChanged,num=%1,_hour=%2,_minute=%3,_second=%4")
                .arg(3).arg(time.hour()).arg(time.minute()).arg(time.second());
        m_host->exec(code,para);
    }

    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="time")
            {
                QDateTime ti=manager->userData(info.m_valueName).toDateTime();
                ti.setTime(time);
                manager->setUserData(info.m_valueName,ti);

            }
        }
    }
}

void QMyTimeEdit::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
}

void QMyTimeEdit::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="timeChanged")
        {
            p->setAttribute("param","timeChanged");
        }
    }
}

void QMyTimeEdit::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}

QDateEditHost::QDateEditHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QDateEditHost::year()
{
    return ((QDateEdit*)m_widget)->date().year();
}

int QDateEditHost::month()
{
    return ((QDateEdit*)m_widget)->date().month();
}

int QDateEditHost::day()
{
    return ((QDateEdit*)m_widget)->date().day();
}

void QDateEditHost::setDate(int year, int month, int day)
{
    ((QDateEdit*)m_widget)->setDate(QDate(year,month,day));
}

QMyDateEdit::QMyDateEdit(QWidget *parent):
    QDateEdit(parent),
    m_timer(new QTimer(this)),
    m_host(new QTimeEditHost(this))
{
    setProperty("className","QDateEdit");
    setProperty("typeName","Date Edit");
    setProperty("typeIcon",":/images/widgets/dateedit.png");
    setDate(QDate::currentDate());

    m_host->addProperty("Attribute",QStringList()<<"date"<<"auto-running"<<"focusPolicy"<<"font");
    m_host->addProperty("Signals",QStringList()<<"dateChanged");
    m_host->addUserProperty(QStringList()<<"date"<<"auto-running");

    connect(m_timer,SIGNAL(timeout()),this,SLOT(timer()));
    m_timer->start(300);

    connect(this,SIGNAL(dateChanged(QDate)),this,SLOT(dateChanged_signals(QDate)));

}

void QMyDateEdit::timer()
{
    if(property("auto-running").toBool())
    {
        if(property("running").toBool())
        {
            setProperty("date",QDate::currentDate());
        }
        else
        {
            PropertyList list=property("property_list").value<PropertyList>();
            foreach(QAbstractProperty *p,list)
            {
                if(p->name()=="date")
                {
                    p->setValue(QDate::currentDate());
                }
            }
        }
    }
}

void QMyDateEdit::dateChanged_signals(const QDate &date)
{
    QString code=property("dateChanged").toString();
    if(code!="")
    {
        QString para=QString("type=dateChanged,num=%1,_year=%2,_month=%3,_day=%4")
                .arg(3).arg(date.year()).arg(date.month()).arg(date.day());
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="date")
            {
                QDateTime ti=manager->userData(info.m_valueName).toDateTime();
                ti.setDate(date);
                manager->setUserData(info.m_valueName,ti);
            }
        }
    }
}

void QMyDateEdit::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
}

void QMyDateEdit::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="dateChanged")
        {
            p->setAttribute("param","dateChanged");
        }
    }
}

void QMyDateEdit::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}

QDateTimeEditHost::QDateTimeEditHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QDateTimeEditHost::year()
{
    return ((QDateTimeEdit*)m_widget)->date().year();
}

int QDateTimeEditHost::month()
{
    return ((QDateTimeEdit*)m_widget)->date().month();
}

int QDateTimeEditHost::day()
{
    return ((QDateTimeEdit*)m_widget)->date().day();
}

int QDateTimeEditHost::hour()
{
    return ((QDateTimeEdit*)m_widget)->time().hour();
}

int QDateTimeEditHost::minute()
{
    return ((QDateTimeEdit*)m_widget)->time().minute();
}

int QDateTimeEditHost::second()
{
    return ((QDateTimeEdit*)m_widget)->time().second();
}

void QDateTimeEditHost::setDate(int year, int month, int day)
{
    ((QDateTimeEdit*)m_widget)->setDate(QDate(year,month,day));
}

void QDateTimeEditHost::setTime(int hour, int minute, int second)
{
    ((QDateTimeEdit*)m_widget)->setTime(QTime(hour,minute,second));
}

void QDateTimeEditHost::setDateTime(int year, int month, int day, int hour, int minute, int second)
{
    ((QDateTimeEdit*)m_widget)->setDateTime(QDateTime(QDate(year,month,day),QTime(hour,minute,second)));
}

QMyDateTimeEdit::QMyDateTimeEdit(QWidget *parent):
    QDateTimeEdit(parent),
    m_timer(new QTimer(this)),
    m_host(new QDateTimeEditHost(this))
{
    setProperty("className","QDateTimeEdit");
    setProperty("typeName","Datetime Edit");
    setProperty("typeIcon",":/images/widgets/datetimeedit.png");
    setDateTime(QDateTime::currentDateTime());


    m_host->addProperty("Attribute",QStringList()<<"dateTime"<<"auto-running"<<"focusPolicy"<<"font");
    m_host->addProperty("Signals",QStringList()<<"dateTimeChanged");
    m_host->addUserProperty(QStringList()<<"dateTime"<<"auto-running");
    connect(m_timer,SIGNAL(timeout()),this,SLOT(timer()));
    m_timer->start(300);

    connect(this,SIGNAL(dateTimeChanged(QDateTime)),this,SLOT(dateTimeChanged_signals(QDateTime)));
}

void QMyDateTimeEdit::timer()
{
    if(property("auto-running").toBool())
    {
        if(property("running").toBool())
        {
            setProperty("dateTime",QDateTime::currentDateTime());
        }
        else
        {
            PropertyList list=property("property_list").value<PropertyList>();
            foreach(QAbstractProperty *p,list)
            {
                if(p->name()=="dateTime")
                {
                    p->setValue(QDateTime::currentDateTime());
                }
            }
        }
    }
}

void QMyDateTimeEdit::dateTimeChanged_signals(const QDateTime &datetime)
{
    QString code=property("dateTimeChanged").toString();
    if(code!="")
    {
        QString para=QString("type=dateTimeChanged,num=%1,_year=%2,_month=%3,_day=%4,_hour=%5,_minute=%6,_second=%7")
                .arg(6).arg(datetime.date().year()).arg(datetime.date().month()).arg(datetime.date().day())
                .arg(datetime.time().hour()).arg(datetime.time().minute()).arg(datetime.time().second());
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="dateTime")
            {
                manager->setUserData(info.m_valueName,this->dateTime());

            }
        }
    }
}

void QMyDateTimeEdit::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
}

void QMyDateTimeEdit::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="dateTimeChanged")
        {
            p->setAttribute("param","dateTimeChanged");
        }
    }
}

void QMyDateTimeEdit::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}
